package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.SkuClassMapper;
import com.heima.pojo.entity.SkuClass;
import com.heima.pojo.vo.PageResponseVo;
import com.heima.service.SkuClassService;
import org.springframework.stereotype.Service;


@Service
public class SkuClassServiceImpl  extends ServiceImpl<SkuClassMapper, SkuClass> implements SkuClassService {

    @Override
    public PageResponseVo<SkuClass> searchSkuClass(Integer pageIndex, Integer pageSize, String className) {
        //创建分页对象
        Page<SkuClass> page = new Page<>(pageIndex, pageSize);
        // 创建查询条件
        LambdaQueryWrapper<SkuClass> queryWrapper = new LambdaQueryWrapper<>();
        //clasName不为空进行模糊查询
        if (className != null && !className.isEmpty()) {
            queryWrapper.like(SkuClass::getClassName, className);
        }
        Page<SkuClass> pageResult = this.page(page, queryWrapper);
        PageResponseVo<SkuClass> result = new PageResponseVo<>();
        result.setPageIndex(pageResult.getCurrent());
        result.setPageSize(pageResult.getSize());
        result.setTotalPage(pageResult.getPages());
        result.setTotalCount(pageResult.getTotal());
        result.setCurrentPageRecords(pageResult.getRecords());
        return result;
    }
    @Override
    public boolean save(SkuClass entity) {
        //判断商品分类名称是否已经存在
        LambdaQueryWrapper<SkuClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkuClass::getClassName, entity.getClassName());
        SkuClass one = this.getOne(queryWrapper);
        if (one != null) {
            return false;
        }
        return super.save(entity);

    }

    @Override
    public SkuClass getById(String classId) {
        //根据指定id查询对应数据
        return super.getById(classId);
    }
}
